# -*- encoding: utf-8 -*-
'''
@File    :   node.py
@Time    :   2021/06/21 13:47:23
@Author  :   Tan yunlin 
@Version :   1.0
@Contact :   3465126952@qq.com
@Desc    :   Node节点，每个节点id都是160bit
'''

# here put the import lib
import random


class Node:
    """
        Node类：定义id（160bit（前120bit代表区域，同一区域前120bit相等，本地40bit）,
        则int范围时0-2^160），节点之间距离也是异或操作，代表在同一区域的距离大小
    """
    def __init__(self, node_id):
        """
        Create a Node instance.
        Args:
            node_id (int): A value between 0 and 2^160，本地只需要考虑40bit
        """
        self.id = node_id  # pylint: disable=invalid-name

    # 获得节点之间的距离：使用XOR度量方式
    def distance_to(self, node):
        """
        Get the distance between this node and another.
        """
        return self.id ^ node.id


# def main():
#     # 测试node类
#     ridone = random.getrandbits(40)  # 获得40bit的随机id
#     ridtwo = random.getrandbits(40)
#     none = Node(ridone)
#     ntwo = Node(ridtwo)
#     print("ridone:" + str(bin(ridone)))  # 按输出二进制形式
#     print("ridtwo：" + str(bin(ridtwo)))
#     print(int(none.distance_to(ntwo)))

# if __name__ == "__main__":
#     main()
